<?php
/**宝贝类型
 * Class TypeController
 * @package Admin\Controller
 * @author leiying
 */

namespace Admin\Controller;

class TypeController extends AdminController {

    /**宝贝类型列表
     * @author leiying
     */
    public function typelist(){

        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_typelist',$get);
        }else{
            if(I('get.p')){
                $this->assign('p',I('get.p'));
            }else{
                session('searchdata_typelist',null);
            }
            $get = $search_data = session('searchdata_typelist');
        }

        if(isset($get['type_name']) && $get['type_name'] != ''){
            $search_data['type_name'] = $get['type_name'];
            $where['a.type_name'] = array('like',"%".$get['type_name']."%");
        }
        if(isset($get['status']) && $get['status'] != ''){
            $search_data['status'] = $get['status'];
            $where['a.status'] = array('eq',$get['status']);
        }
        $count = M('goods_type')->alias("a")->where($where)->count();

        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出

        $this->assign('page',$show);// 赋值分页输出

        $lists = M('goods_type')
            ->field("a.*,count(b.type_id) as attr_count")
            ->alias("a")
            ->join(C('DB_PREFIX')."attribute b on a.id=b.type_id",'left')
            ->where($where)
            ->group("a.id")
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();

//        echo M()->getLastsql();

        $this->assign('lists',$lists);
        $p = I('get.p',"1");
        $this->assign("p",$p);

        $this->assign('searchs',$search_data);

        $this->display();

    }

    /**添加宝贝类型
     * @author leiying
     */
    public function typeAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');

            //处理参数
            $data['type_name'] = trim($post['type_name']);
            if($data['type_name'] == ''){
                $this->error('类型名称不能为空');
            }
            $data['status'] = $post['status'];

            //验证通过了
            $res = M('goods_type')->add($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->display();
        }
    }

    /**编辑宝贝类型
     * @author leiying
     */
    public function typeEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            $id = $post['id'];
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }

            //处理参数
            $data['type_name'] = trim($post['type_name']);
            if($data['type_name'] == ''){
                $this->error('类型名称不能为空');
            }
            $data['status'] = $post['status'];

            $res = M('goods_type')->where(array('id'=>$id))->save($data);
            if(is_numeric($res)){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $id = I('get.id');
            if($id){
                $data = M('goods_type')->where(array('id'=>$id))->find();
                $this->assign('data',$data);
                $p = I('get.p',"1");
                $this->assign('p',$p);
                $this->display();
            }else{$this->error("请先选择操作数据");}

        }
    }

    /**热门品牌推荐
     * @param null $method
     * @author leiying
     */
    public function updateStatus($method=null){

        $id = I('post.id');
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }

        switch ( strtolower($method) ){
            case 'down':
                $res = M("brand")->where(array('brand_id'=>$id))->save(array('is_hot'=>0));
                if($res){
                    $this->success("设置不推荐成功!");
                }else{
                    $this->error("操作失败!");
                }
                break;
            case 'up':
                $res = M("brand")->where(array('brand_id'=>$id))->save(array('is_hot'=>1));
                if($res){
                    $this->success("设置推荐成功!");
                }else{
                    $this->error("操作失败!");
                }
                break;
            default:
                $this->error($method.'参数非法');
        }
    }


    /**删除宝贝类型
     * @author leiying
     */
    public function deleteType(){
        if(IS_POST){
            $id = I('post.id');
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }

            $res = M('goods_type')->where(array('id'=>$id))->delete();
            if($res){
                M('attribute')->where(array('type_id'=>$id))->delete();
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('非法操作');
        }
    }

}
